package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Asignaturaxexamen;


public class AsignaturaxexamenDAO implements IAsignaturaxexamenDAO {
    private static final Log log = LogFactory.getLog(AsignaturaxexamenDAO.class);

    // property constants
    //public static final Integer  ASIGXEXAMIDN = "asigxexamidn";
    public static final String ASIGXEXAMIDN = "asigxexamidn";

    //public static final Integer  ASIGXEXAMORDN = "asigxexamordn";
    public static final String ASIGXEXAMORDN = "asigxexamordn";

    //public static final Integer  ASIGXEXAMPREGN = "asigxexampregn";
    public static final String ASIGXEXAMPREGN = "asigxexampregn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Asignaturaxexamen Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Asignaturaxexamen instance) {
        log.debug("saving Asignaturaxexamen instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Asignaturaxexamen Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Asignaturaxexamen instance) {
        log.debug("deleting Asignaturaxexamen instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Asignaturaxexamen Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Asignaturaxexamen instance) {
        log.debug("updating Asignaturaxexamen instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Asignaturaxexamen findById(Integer id) {
        log.debug("finding Asignaturaxexamen instance with id: " + id);

        try {
            Asignaturaxexamen instance = (Asignaturaxexamen) getSession()
                                                                 .get(Asignaturaxexamen.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Asignaturaxexamen failed", re);
            throw re;
        }
    }

    public List<Asignaturaxexamen> findByExample(Asignaturaxexamen instance) {
        log.debug("finding Asignaturaxexamen instance by example");

        try {
            List results = getSession().createCriteria("Asignaturaxexamen")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Asignaturaxexamen entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Asignaturaxexamen> found by query
        */
    public List<Asignaturaxexamen> findByProperty(String propertyName,
        Object value) {
        log.debug("finding Asignaturaxexamen instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from Asignaturaxexamen as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Asignaturaxexamen> findByAsigxexamidn(Object asigxexamidn) {
        return findByProperty(ASIGXEXAMIDN, asigxexamidn);
    }

    public List<Asignaturaxexamen> findByAsigxexamordn(Object asigxexamordn) {
        return findByProperty(ASIGXEXAMORDN, asigxexamordn);
    }

    public List<Asignaturaxexamen> findByAsigxexampregn(Object asigxexampregn) {
        return findByProperty(ASIGXEXAMPREGN, asigxexampregn);
    }

    /**
    * Find all Asignaturaxexamen entities.
    *
    * @return List<Asignaturaxexamen> all Asignaturaxexamen instances
    */
    public List<Asignaturaxexamen> findAll() {
        log.debug("finding all Asignaturaxexamen instances");

        try {
            String queryString = "from Asignaturaxexamen";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Asignaturaxexamen> findByCriteria(String whereCondition) {
        log.debug("finding Asignaturaxexamen " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Asignaturaxexamen model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Asignaturaxexamen> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Asignaturaxexamen failed", re);
            throw re;
        }
    }

    public List<Asignaturaxexamen> findPageAsignaturaxexamen(
        String sortColumnName, boolean sortAscending, int startRow,
        int maxResults) {
        log.debug("finding Asignaturaxexamen findPageAsignaturaxexamen");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Asignaturaxexamen model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Asignaturaxexamen model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberAsignaturaxexamen() {
        log.debug("finding Asignaturaxexamen count");

        try {
            String queryString = "select count(*) from Asignaturaxexamen model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    
    public Integer numeroDePreguntasPorSesion(Integer examidn){
    	 try {
             String queryString = "SELECT SUM (asignaturaxexamen.asigxexampregn) FROM asignaturaxexamen INNER JOIN asignatura ON (asignatura.asigidn=asignaturaxexamen.asigidn) WHERE asignaturaxexamen.examidn = " + examidn;
             Query queryObject = getSession().createQuery(queryString);
             
             return (Integer) queryObject.list().get(0);
         } catch (RuntimeException re) {
             throw re;
         }
    }
}
